﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerAutoBusinessDao : IAutoBusinessDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="autoBusiness">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(AutoBusiness autoBusiness)
        {
            return new object[]  
            {
                "@AutoBusinessID", autoBusiness.AutoBusinessId,
                "@AutoBusinessCode", autoBusiness.AutoBusinessCode,
                "@AutoBusinessName", autoBusiness.AutoBusinessName,
                "@RefTypeID", autoBusiness.RefTypeId,
                "@VoucherTypeID", autoBusiness.VoucherTypeId,
                "@DebitAccountNumber", autoBusiness.DebitAccountNumber,
                "@CreditAccountNumber", autoBusiness.CreditAccountNumber,
                "@Description", autoBusiness.Description,
                "@IsActive", autoBusiness.IsActive,
                 "@UnitID", autoBusiness.UnitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, AutoBusiness> Make = reader =>
          new AutoBusiness
          {
              AutoBusinessId = reader["AutoBusinessID"].AsInt(),
              AutoBusinessCode = reader["AutoBusinessCode"].AsString(),
              AutoBusinessName = reader["AutoBusinessName"].AsString(),
              RefTypeId = reader["RefTypeID"].AsInt(),
              VoucherTypeId = reader["VoucherTypeID"].AsInt(),
              DebitAccountNumber = reader["DebitAccountNumber"].AsString(),
              CreditAccountNumber = reader["CreditAccountNumber"].AsString(),
              Description = reader["Description"].AsString(),
              IsActive = reader["IsActive"].AsBool(),
              UnitId = reader["UnitID"].AsLong()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<AutoBusiness> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_AutoBusiness";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_AutoBusiness";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the autoBusiness.
        /// </summary>
        /// <param name="autoBusiness">The autoBusiness.</param>
        /// <returns></returns>
        public string InsertAutoBusiness(AutoBusiness autoBusiness)
        {
            const string sql = "uspInsert_AutoBusiness";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(autoBusiness));
        }

        /// <summary>
        /// Updates the autoBusiness.
        /// </summary>
        /// <param name="autoBusiness">The autoBusiness.</param>
        /// <returns></returns>
        public string UpdateAutoBusiness(AutoBusiness autoBusiness)
        {
            const string sql = "uspUpdate_AutoBusiness";
            return Db.Update(sql, CommandType.StoredProcedure, Take(autoBusiness));
        }

        /// <summary>
        /// Deletes the autoBusiness.
        /// </summary>
        /// <param name="autoBusiness">The autoBusiness.</param>
        /// <returns></returns>
        public string DeleteAutoBusiness(AutoBusiness autoBusiness)
        {
            const string sql = @"uspDelete_AutoBusiness";
            object[] parms = { "@AutoBusinessID", autoBusiness.AutoBusinessId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public AutoBusiness GetAutoBusiness(int autoBusinessId)
        {
            const string sql = @"uspGet_AutoBusiness_ByID";
            object[] parms = { "@AutoBusinessID", autoBusinessId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<AutoBusiness> GetAutoBusinessByUnit(int unitId)
        {
            const string sql = @"uspGet_AutoBusiness_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure,Make, parms);
        }
    }
}
